package com.ssm.chageunit.logmanager;

import com.ssm.changeunit.ChangeUnit;
import com.ssm.changeunit.ChangeUnitFactory;
import com.ssm.changeunit.ChangeUnitItem;
import com.ssm.changeunit.CreateException;
import com.ssm.changeunit.persistance.ChangeUnitDAO;
import com.ssm.changeunit.persistance.ChangeUnitDTO;
import com.ssm.changeunit.persistance.ChildDAO;
import com.ssm.changeunit.persistance.ChildDTO;
import com.ssm.changeunit.persistance.ParentDAO;
import com.ssm.changeunit.persistance.PersistanceException;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;


class ItemMouseHandler implements MouseListener {

    public void mouseClicked(MouseEvent e) {
        System.out.println("clicked ITem");
    }
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}
public class Logmanager extends JFrame implements MouseListener{
        protected final static String names[] = { "    ChangeUnitId ", "    UserId",
                        "    Time", "    Log", "    Item" ,"    source"};
        private String values[];
        private int childSize,a,temp,Icon,ItemlistSize,flag,temp2,childsizeflag;
        private Container container;
        private GridBagLayout layout;
        private GridBagConstraints constraints;
        private JTextArea textArea[];
        private JLabel labels[],IconLabel[],emptyLabel[],ItemLabel[],emptyItemLabel[];
        protected JTextField fields[];

        protected int size;
        public static final int VersionNumber = 0, UserId = 1, Time = 2,
                        Log = 3, Itme = 4, source = 5;

        public void OpenFileFirsttime(){
            flag=1;
            try{
                ChangeUnitDAO dao = new ChangeUnitDAO();
                java.util.List<ChangeUnitDTO> list = dao.list();
                ChangeUnitDTO changeUnitDTO = list.get(0);
                ChangeUnit changeUnit = new ChangeUnitFactory().createProduct(changeUnitDTO.getChangeUnitId());
                OpenFile(changeUnitDTO.getChangeUnitId());
            }
            catch(CreateException ex) {ex.printStackTrace();}
            catch(PersistanceException ex) {ex.printStackTrace();} 
        }

        public void OpenFile(String changeUnitId) {
            
            try {
                    ChangeUnit changeUnit = new ChangeUnitFactory().createProduct(changeUnitId);
                    for(int temp=0;temp<2;temp++){
                    java.util.List<ChangeUnitItem> lists = changeUnit.getChangeUnitItems();

                    ItemlistSize = lists.size();
                    }

                    java.util.List<ChangeUnitItem> lists = changeUnit.getChangeUnitItems();
                    ItemLabel = new JLabel[ItemlistSize];
                    if(ItemlistSize==0)
                        temp2=0;

                    int temp=0;
                    for(ChangeUnitItem changeUnitItem : lists) {
                        String filePath = changeUnitItem.getFilePath();
                        ItemLabel[temp]=new JLabel(filePath);
                        ItemLabel[temp].setName(filePath);
                        ++temp;
                    }

                    values = new String[5];

                    values[0] = String.valueOf(changeUnit.getChangeUnitId());
                    values[1] = String.valueOf(changeUnit.getUserId());
                    values[2] = String.valueOf(changeUnit.getTime());
                    values[3] = String.valueOf(changeUnit.getLog());
                    values[4] = String.valueOf("this is source");

                    for (int i = 0 ; i <3 ; i++) {
                        fields[i].setText(values[i]);
                        }

                    textArea[0].setText(values[3]);
                    textArea[1].setText(values[4]);
                    
                    emptyItemLabel = new JLabel[4-ItemlistSize];
                
                    for(int count = 0;count<(4-ItemlistSize);count++)
                        emptyItemLabel[count]= new JLabel("  ");
                    
                    constraints.weightx = 1000;
                    constraints.weighty = 0;
                    constraints.fill = GridBagConstraints.BOTH;
                    for(int count = 0 ; count < ItemlistSize ; count++){
                        addComponent(ItemLabel[count],6+count,14,2,1);
                    }
                    for(int count4 = 0;count4<(4-ItemlistSize);count4++){
                        addComponent(emptyItemLabel[count4],6+ItemlistSize+count4,14,2,1);
                    }
                    
                    repaint();
                    drawChild(changeUnitId);
            }
            catch(CreateException ex) {}
            catch(PersistanceException ex) {} 
        }

        public void drawChild(String changeUnitId) {
            try {
                ChildDAO childDAO = new ChildDAO();
                temp =0;
                java.util.List<ChildDTO> childDTOs = childDAO.findByChangeUnitId(changeUnitId);

                for(ChildDTO childDTO : childDTOs) {
                    drawChild(childDTO.getChildChangeUnitId());
                }
                temp++;
            }
            catch (PersistanceException ex) {
                Logger.getLogger(Logmanager.class.getName()).log(Level.SEVERE, null, ex);
            }
            childSize = temp;
        }
        public Logmanager() {
                super("Log Manager");
                JMenu fileMenu = new JMenu("File");
                fileMenu.setMnemonic('F');

                JMenuItem exitItem = new JMenuItem("Exit");
                exitItem.setMnemonic('x');
                fileMenu.add(exitItem);
                exitItem.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                                System.exit(0);
                        }
                });

                JMenuBar bar = new JMenuBar();
                bar.add(fileMenu);

                setJMenuBar(bar);

                JMenu aboutMenu = new JMenu("About");
                aboutMenu.setMnemonic('b');

                JMenuItem aboutItem = new JMenuItem("About...");
                aboutItem.setMnemonic('A');
                aboutMenu.add(aboutItem);

                aboutItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        JOptionPane.showMessageDialog(
                                Logmanager.this,
                                "11 Team \n Mini Project\n This is a Logmanager \n Made by 박만규",
                                "About", JOptionPane.PLAIN_MESSAGE);
                        }
                });

                bar.add(aboutMenu);

                setJMenuBar(bar);
                container = getContentPane();

                layout = new GridBagLayout();
                container.setLayout(layout);

                labels = new JLabel[6];
                fields = new JTextField[3];
                textArea = new JTextArea[2];
                emptyLabel = new JLabel[25-childSize];

                for (int count = 0; count < 6; count++)
                        labels[count] = new JLabel(names[count]);

                for (int count = 0; count < 3; count++) {
                        fields[count] = new JTextField();
                        fields[count].setEditable(false);
                }

                constraints = new GridBagConstraints();

                textArea[0] = new JTextArea("");
                textArea[0].setEditable(false);

                textArea[1] = new JTextArea("");
                textArea[1].setEditable(false);

                OpenFileFirsttime();

                constraints.weightx = 0;
                constraints.weighty = 0;
                constraints.fill = GridBagConstraints.BOTH;
                addComponent(labels[0], 0, 12, 2, 1);
                addComponent(labels[1], 1, 12, 2, 1);
                addComponent(labels[2], 2, 12, 2, 1);
                addComponent(labels[3], 3, 12, 2, 1);
                addComponent(labels[4], 7, 12, 2, 1);
                addComponent(labels[5], 11, 12, 2, 1);

                constraints.weightx = 0;
                constraints.weighty = 0;
                constraints.fill = GridBagConstraints.BOTH;
                addComponent(fields[0], 0, 14, 2, 1);
                addComponent(fields[1], 1, 14, 2, 1);
                addComponent(fields[2], 2, 14, 2, 1);
                JScrollPane vbal=new JScrollPane(textArea[0]);
                addComponent(vbal, 3, 14, 2, 3);

                if(childSize<=25){
                    IconLabel = new JLabel[childSize];
                    for(int count =0;count <childSize;count++)
                        IconLabel[count]= new JLabel();
                    for(int count = 0;count < (25-childSize);count++)
                        emptyLabel[count]=new JLabel("      ");
                }

                else{
                    IconLabel = new JLabel[childSize];
                    for(int count = 0;count <childSize;count++)
                        IconLabel[count]=new JLabel();
                }
                if(childSize<=25){
                    constraints.weightx = 0;
                    constraints.weighty = 0;
                    constraints.fill = GridBagConstraints.BOTH;
                    for(a=0;a<childSize;a++){
                        addComponent(IconLabel[a],a,0,12,1);
                    }
                    for(a=0;a<(25-childSize);a++){
                        addComponent(emptyLabel[a],childSize+a,0,12,1);
                    }

                    constraints.weightx = 0;
                    constraints.weighty = 0;
                    constraints.fill = GridBagConstraints.BOTH;
                    JScrollPane vbal2=new JScrollPane(textArea[1]);
                    addComponent(vbal2, 10, 14, 2, 15);
                }
                else{
                    constraints.weightx = 0;
                    constraints.weighty = 0;
                    constraints.fill = GridBagConstraints.BOTH;
                    for(a=0;a<childSize;a++){
                        addComponent(IconLabel[a],a,0,12,1);
                    }
                    constraints.weightx = 0;
                    constraints.weighty = 0;
                    constraints.fill = GridBagConstraints.BOTH;
                    JScrollPane vbal2=new JScrollPane(textArea[1]);
                    addComponent(vbal2, 10, 14, 2, 15+(childSize-25));
                }
            
                if(childSize<=25)
                    setSize(800,515);
                else
                    setSize(800,500+(20*(childSize-25)));
                setResizable(false);
                setVisible(true);
        }

        private void addComponent(Component component, int row, int column,
                        int width, int height) {
                constraints.gridx = column;
                constraints.gridy = row;

                constraints.gridwidth = width;
                constraints.gridheight = height;

                layout.setConstraints(component, constraints);
                container.add(component);
        }

        public void paint(Graphics g)
        {
                super.paint(g);

                Icon red=new ImageIcon("resource\\red.jpg");
                Icon blue=new ImageIcon("resource\\blue.jpg");
                Icon yellow=new ImageIcon("resource\\yellow.jpg");
                if(flag==1){
                    for(Icon=0;Icon<childSize;Icon++){
                        IconLabel[0].setIcon(red);
                        IconLabel[0].setName(String.valueOf(values[0]));
                        IconLabel[0].setText("First projectFile");
                        if(Icon>0){
                            IconLabel[Icon].setIcon(yellow);
                            try {
                                ChildDAO childDAO = new ChildDAO();
                                java.util.List<ChildDTO> childDTOs = null;
                                try {
                                    childDTOs = childDAO.findByChangeUnitId(IconLabel[Icon - 1].getName());
                                } catch (PersistanceException ex) {
                                    ex.printStackTrace();
                                }
                                childsizeflag=1;

                                for(ChildDTO childDTO : childDTOs) {
                                    String childChangeUnitId = childDTO.getChildChangeUnitId();
                                    ChangeUnit childChangeUnit = new ChangeUnitFactory().createProduct(childChangeUnitId);
                                    if(childsizeflag>=2){
                                        ++Icon;
                                        IconLabel[Icon-1].setIcon(blue);
                                        IconLabel[Icon].setIcon(blue);
                                        IconLabel[Icon].setName(childChangeUnitId);
                                        IconLabel[Icon].setText(childChangeUnit.getLog());
                                    }
                                    else{
                                        IconLabel[Icon].setName(childChangeUnitId);
                                        IconLabel[Icon].setText(childChangeUnit.getLog());
                                    }
                                    childsizeflag++;
                                }

                            } catch (Exception ex) {
                                Logger.getLogger(Logmanager.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    IconLabel[Icon].addMouseListener(this);
                    }
                }
                else{
                    for(Icon=0;Icon<childSize;Icon++)
                        IconLabel[Icon].addMouseListener(this);

                    for(int count = 0 ; count < ItemlistSize ; count++){
                        temp2=ItemlistSize;
                        ItemLabel[count].setText(ItemLabel[count].getName());
                        ItemLabel[count].addMouseListener(new ItemMouseHandler());
                    }

                    for(int count = 0 ; count<(4-ItemlistSize) ; count++){
                        emptyItemLabel[count].setText(" ");
                    }
                }
        }

        public void mouseClicked(MouseEvent e){
                flag=2;
                if(!e.isConsumed()) {
                    if(Icon==0){
                        OpenFileFirsttime();
                    }
                    else if(Icon!=0){
                        for(int temp=0;temp<temp2;temp++)
                            ItemLabel[temp].setText("");

                        OpenFile(((JLabel)e.getSource()).getName());
                    }
                    e.consume();
                }
        }

    public void mouseEntered(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseMoved(MouseEvent e){}

        public static void main(String args[]) {
                Logmanager application = new Logmanager();
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
}